import { _decorator, Component, Node, sys, resources, EventMouse, instantiate, Prefab, assetManager, AssetManager, Vec3 } from "cc";
// import { ProtoMgr } from '../Mangaers/ProtoMgr';
// import protobufjs from 'protobufjs';
import msg from "../Msg/msgpb.js";
import NetEventMgr from "./NetEventMgr";
import EventMgr from "../Mangaers/EventMgr";
import NetMgr from "../Mangaers/NetMgr";
import UiManager from "../UI/UiManager";
import { plog } from "../Utils/Tools";
import { ParamUI } from "../UI/ParamUI";
import { DataMgr } from "./DataMgr";
// import mg from 'msg';
// import { msg } from '../Msg/gao.d';
const { ccclass, property } = _decorator;



@ccclass("GameApp")
export class GameApp extends Component {
    public static Instance: GameApp = null;
    NodeBase: Node = null;
    NodeSprite: Node = null;
    NodeUIBase: Node = null;

    onLoad(): void {

        if (GameApp.Instance === null) {
            GameApp.Instance = this;
        } else {
            this.destroy();
            return;
        }
        this.node.addComponent(NetEventMgr);
        this.node.addComponent(UiManager);
        this.node.addComponent(DataMgr);
        this.NodeUIBase = this.node.getChildByName("NodeUI");
        this.NodeBase = this.node.getChildByName("NodeBase");
        this.NodeSprite = this.node.getChildByName("NodeSprite");
        this.NodeSprite.on(Node.EventType.TOUCH_START, this.onTouchSprite, this);
    }

    public onConnect(uname, udata): void {
        // plog("net_connect ===================")
        // UiManager.Instance.OpenUI("login", () => {
        //     // CoverManager.Instance.Hide()
        // })
        /**
            plog("---onConnect---", uname, udata)
            const mr = msg.AccountRegistR
            const ma = msg.AccountRegistA
            let m = mr.create()
            m.Accout = "002"
            m.Password = "111"
            // let bs = mr.encode(m).finish()
            // plog(" bs...", bs)
            // NetEventMgr.Instance.onRecvData(" bs...", bs.buffer)
            NetEventMgr.Instance.SendMessage(mr.name, ma.name, mr.encode(m).finish(), (mc: proto.msg.MsgConfig, data) => {
    
                // plog("++++mc+++", mc) 
                // plog("++++data+++", data)
                let a = ma.decode(data);
                // plog("-=-= mc", mc)
                // plog("-=-= a", a)
            })
            /**/
    }

    public onTouchSprite(event: EventMouse): void {
        // sprite 层按键 临时的
        plog("onTouchSprite", event); //按一下开始联网
        // this.NodeSprite.off(Node.EventType.TOUCH_START, this.onTouchSprite, this)

        UiManager.Instance.AddUI(this.NodeUIBase, "login", null, (pm) => {
            plog("!!!!!!! callback base: ", pm);
        });

        // UiManager.Instance.AddCover(this.NodeUIBase, 2, 500)

        // EventMgr.Instance.add_event_listenner("net_connect", this, this.onConnect);
        // NetMgr.Instance.StartConnect()
        // CoverManager.Instance.Show(1)
    }

    onDestroy() { }

    public GameStart(): void {
        this.EnterGameSence();
    }

    public EnterGameSence(): void {
        localStorage.setItem("name", "aaa");
        // NetMgr.Instance.StartConnect()
        /**
            // var m0 = { p: { name: "a", Id: 9 } }
            // var buf = ProtoMgr.Instance.SerializeMsg("GetPlayerA", m0)
            // plog(buf)
            // var m = ProtoMgr.Instance.DeserializeMsg("GetPlayerA", buf)
            // plog("--11-", m)
            // plog("---", m.p)
    
            // plog("msg.ErrCode", msg.ErrCode)
            // localStorage.setItem("name", "aaa")
            //释放资源
            // let val = localStorage.getItem("name")
            plog("val:", val)
            // plog("!#!#!#!#", protobufjs);
            // msg.
            plog(msg.AccountLoginA);
            let p = new msg.Player()
            p.Name = "a"
            p.Id = 9
            let j = p.toJSON()
            plog("jjj  ", j)
            let bs = msg.Player.encode(p).finish();
            plog("bs --", bs)
            let p2 = msg.Player.decode(bs)
            plog("p2 --", p2)
            plog("-----", msg.Player.name)
            let er = msg.ErrCode
            plog("er --", er.Err_FindData)
    
            /**/
        // let m = this.getComponent(NetEventMgr)
        // m.run(null, () => { this.test })
        // m.run(null, (n) => this.test(n))
    }

    public GetSceneState(): number {
        return 0;
    }
    // p() {
    //     plog("--111------...")
    // }

    // test(n) {
    //     plog("---------fn ", n, this)
    //     return 2
    // }
}
